# views.py
from django.shortcuts import render_to_response, render
from django.template import RequestContext
from django.http import HttpResponseRedirect
#from forms import FarmerForm
from gleanEvent.models import Farm
from django.core.exceptions import ObjectDoesNotExist

import time
import calendar
from datetime import date, timedelta
from gleanEvent.models import GleanEvent


# import the logging library
import logging

# Get an instance of a logger
logger = logging.getLogger(__name__)


def index_view(request):
	return HttpResponseRedirect("/static/index.html")

def main_view(request, name=None, change=None):

	#if request.user.get_profile().access_level == 'FA':
	#	return render(request, 'farmer.html', {'form': FarmerForm,})
	
	#elif request.user.get_profile().access_level == 'VO':
    
	return HttpResponseRedirect('/calendar')
	"""	
	elif request.user.get_profile().access_level == 'MO':
		return render_to_response("cal/memberorg.html", dict(eventlst=eventlst, name=name, year=year, month=month, user=request.user,
                            month_days=lst, mname=mnames[month-1], reminders=reminders(request)),
                            context_instance=RequestContext(request))
	"""
    

def farmer_view(request):
    if request.method == 'POST': # If the form has been submitted...
        form = FarmerForm(request.POST) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
            # Process the data in form.cleaned_data
            # ...
            return HttpResponseRedirect('/farmer/') # Redirect after POST
    else:
        form = FarmerForm() # An unbound form

    return render(request, 'farmer.html', {
        'form': form,
    })
    
def farmer_edit(request):
	try:
		farm = Farm.objects.get(user__username=request.user.username)
		print farm.pk, "*********"
	except ObjectDoesNotExist:
		print "Houston we have a problem"
	if request.method == 'POST': # If the form has been submitted...
		form = FarmerForm(request.POST, instance=farm) # A form bound to the POST data
		if form.is_valid(): # All validation rules pass
            # Process the data in form.cleaned_data	
			form.save()
			return HttpResponseRedirect('/farmer/') # Redirect after POST
	else:
		form = FarmerForm(instance=farm) 
		#form = FarmerForm() 

	return render(request, 'farmer.html', {
        'form': form, 'edit_info': "yes" 
    })
